package com.it.controller;

import com.it.Bean.Guestbook;
import com.it.service.GuestbookService;
import com.it.service.impl.GuestbookServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;



public class ManageServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //我们只需 要在doGet中获取到请求的参数q=list，然后根据获取到的参数来决定调用哪一个方法
        String method = req.getParameter("q");
        if("delete".equals(method)) {
            delete(req,resp);
        }else if("update".equals(method)){
            update(req,resp);
        }else {
            list(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    //查询所有信息的方法
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        //调用业务层查询方法
        GuestbookService service = new GuestbookServiceImpl();
        //获取操作标识
        String flag = req.getParameter("flag");
        //如果为添加
        if ("add".equals(flag)){
            String inputName = req.getParameter("inputName");
            String inputPhone = req.getParameter("inputPhone");
            String inputEmail = req.getParameter("inputEmail");
            String inputTitle = req.getParameter("inputTitle");
            String inputContent = req.getParameter("inputContent");
            String inputCreatedTime = req.getParameter("inputCreatedTime");
            // 创建对象
            Guestbook gb = new Guestbook(0, inputName, inputPhone, inputEmail, inputTitle, inputContent, inputCreatedTime);

            Integer successflag = service.AddMessage(gb);
            System.out.println(successflag==1?"添加成功":"添加失败");
            //跳转


        }
        if ("delete".equals(flag)){
            String delId = req.getParameter("del_id");
            int del_id = Integer.parseInt(delId);
            Integer successflag = service.DeleteMessage(del_id);
            System.out.println(successflag==1?"添加成功":"添加失败");
            //跳转
        }


        List<Object> list = service.queryInfo();

        //将查询到的list集合保存到服务器端
        req.setAttribute("list",list);
        //之后就可以将这个请求转发给list.jsp
        req.getRequestDispatcher("/list.jsp").forward(req,resp);

    }

    //删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    //修改
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
    //添加
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

}
